package com.qf.forumproject.controller;

import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageInfo;
import com.qf.forumproject.utils.PageResult;
import com.qf.forumproject.utils.Result;
import com.qf.forumproject.utils.StatusCode;
import com.qf.forumproject.entity.QfBlog;
import com.qf.forumproject.service.QfBlogService;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (QfBlog)控制层
 *
 * @author protagonist
 * @since 2022-07-23 11:37:12
 */
@RestController
@Slf4j
@RequestMapping("/qfBlog")
public class QfBlogController {
    /**
     * 服务对象
     */
    @Resource
    private QfBlogService qfBlogServiceImpl;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/get/{id}")
    public Result selectOne(@PathVariable("id") Integer id) {
        QfBlog result = qfBlogServiceImpl.selectById(id);
        if(Objects.nonNull(result)){
            return new Result<>(true,StatusCode.OK,"查询成功",result);
        }
        return new Result<>(true,StatusCode.ERROR,"查询失败");
    }
    
    /**
     * 新增一条数据
     *
     * @param qfBlog 实体类
     * @return Result对象
     */
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody QfBlog qfBlog) {
        int result = qfBlogServiceImpl.insert(qfBlog);
        if (result > 0) {
          return new Result<>(true,StatusCode.OK,"新增成功",result);
        }
        return new Result<>(true,StatusCode.ERROR,"新增失败"); 
    }

    /**
     * 修改一条数据
     *
     * @param qfBlog 实体类
     * @return Result对象
     */
    @PutMapping(value = "/update")
    public Result update(@RequestBody QfBlog qfBlog) {
        QfBlog result = qfBlogServiceImpl.update(qfBlog);
        if (Objects.nonNull(result)) {
          return new Result<>(true,StatusCode.OK,"修改成功",result);
        }
        return new Result<>(true,StatusCode.ERROR,"修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param id 主键
     * @return Result对象
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int result = qfBlogServiceImpl.deleteById(id);
        if (result > 0) {
          return new Result<>(true,StatusCode.OK,"删除成功",result);
        }
        return new Result<>(true,StatusCode.ERROR,"删除失败");
    }

    /**
     * 查询全部
     *
     * @return Result对象
     */
    @GetMapping(value = "/selectAll")
    public Result<List<QfBlog>> selectAll() {
        List<QfBlog> qfBlogs = qfBlogServiceImpl.selectAll();
        if (CollectionUtils.isEmpty(qfBlogs)) {
            return new Result<>(true,StatusCode.ERROR,"查询全部数据失败");       
        }
        return new Result<>(true,StatusCode.OK,"查询全部数据成功",qfBlogs);
        
    }

    /**
     * 分页查询
     *
     * @param current 当前页  第零页和第一页的数据是一样
     * @param size 每一页的数据条数
     * @return Result对象
     */
    @GetMapping(value = "/selectPage/{current}/{size}")
    public Result selectPage(@PathVariable("current") Integer current,@PathVariable("size") Integer size) {
        PageInfo<QfBlog> page = qfBlogServiceImpl.selectPage(current, size);
        if (Objects.nonNull(page)) {
            return new Result<>(true,StatusCode.OK,"分页条件查询成功",new PageResult<>(page.getTotal(),page.getList()));
        }
        return new Result<>(true,StatusCode.ERROR,"分页查询数据失败");
    }
    
}
